package com.example.teacherservice.repository;

import com.example.teacherservice.entity.StuCou;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface StuCouRepository extends JpaRepository<StuCou, Long> {

    boolean existsByStuIdAndCouId(Long stuId, Long couId);

    @Transactional
    @Modifying
    @Query("DELETE FROM StuCou sc WHERE sc.stuId = ?1 AND sc.couId = ?2")
    int deleteByStuIdAndCouId(Long stuId, Long couId);

    /**
     * 根据学生ID查询购物车中的课程ID列表
     * @param stuId 学生ID
     * @return 课程ID列表
     */
    @Query("SELECT sc.couId FROM StuCou sc WHERE sc.stuId = ?1")
    List<Long> findCourseIdsByStudentId(Long stuId);

    @Modifying
    @Transactional
    @Query("DELETE FROM StuCou sc WHERE sc.stuId = :stuId AND sc.couId IN :couIds")
    void deleteByStuIdAndCouIdIn(@Param("stuId") Long stuId, @Param("couIds") List<Long> couIds);

    // 根据学生ID和课程ID列表查找购物车记录
    @Query("SELECT sc FROM StuCou sc WHERE sc.stuId = :stuId AND sc.couId IN :couIds")
    List<StuCou> findByStuIdAndCouIdIn(@Param("stuId") Long stuId, @Param("couIds") List<Long> couIds);

    // 根据学生ID查找所有购物车记录
    List<StuCou> findByStuId(Long stuId);


}